﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Øving3Blackjack
{
    class CardsInPlay
    {
        private List<Card> playerCards = new List<Card>();
        private List<Card> dealerCards = new List<Card>();
        private List<Card> splitCards = new List<Card>();
        private List<Card> splitCards2 = new List<Card>();
        private Points points = new Points();

        private int dealerValue;
        private int playerValue;
        private int splitValue;
        private int splitValue2;

        private Boolean playerSplit = false;
        private Boolean playerDone = false;

        public void paintAllCards(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if ((playerCards.Count == 0) || (dealerCards.Count == 0))
            {
                g.Clear(Color.BurlyWood);
                return;
            }
            if (!playerSplit)
            {
                for (int counter = 0; counter < playerCards.Count; counter++)
                {
                    playerCards.ElementAt(counter).paintSelf(sender, e, points.getPoint(true, 0, playerCards.Count, counter + 1));
                }
            }
            else
            {
                for (int counter = 0; counter < playerCards.Count; counter++)
                {
                    playerCards.ElementAt(counter).paintSelf(sender, e, points.getPoint(true, 1, playerCards.Count, counter + 1));
                }
                for (int counter = 0; counter < splitCards.Count; counter++)
                {
                    splitCards.ElementAt(counter).paintSelf(sender, e, points.getPoint(true, 2, splitCards.Count, counter + 1));
                }
            }
            if (!playerDone)
            {
                dealerCards.ElementAt(0).paintSelf(sender, e, points.getPoint(false, 0, 2, 1));
                g.DrawImage(Image.FromFile("cards/hidden.gif"), points.getPoint(false, 0, 2, 2));
            }
            else
            {
                for (int counter = 0; counter < dealerCards.Count; counter++)
                {
                    dealerCards.ElementAt(counter).paintSelf(sender, e, points.getPoint(false, 0, dealerCards.Count, counter + 1));
                }
            }
        }
        public Boolean PlayerDone
        {
            get { return playerDone; }
            set { playerDone = value; }
        }
        public Boolean PlayerSplit
        {
            get { return playerSplit; }
            set { playerSplit = value; }
        }
        public void addPlayerCard(Card card)
        {
            playerCards.Add(card);
        }
        public void addDealerCard(Card card)
        {
            dealerCards.Add(card);
        }
        public void addSplitCard(Card card)
        {
            splitCards.Add(card);
        }
        public void split()
        {
            playerValue = 0;
            splitValue = 0;
            splitCards.Add(playerCards.ElementAt(1));
            playerCards.RemoveAt(1);
            
        }
        public void emptyHands()
        {
            playerCards.RemoveRange(0, playerCards.Count);
            dealerCards.RemoveRange(0, dealerCards.Count);
            splitCards.RemoveRange(0, dealerCards.Count);
            splitCards2.RemoveRange(0, dealerCards.Count);
            playerValue = 0;
            dealerValue = 0;
            splitValue = 0;
            splitValue2 = 0;
        }

        public Boolean playerHasBJ()
        {
            if (playerSplit == true)
            {
                return false;
            }
            for (int counter = 0; counter < 2; counter++)
            {
                if (playerCards.ElementAt(counter).Ace == true)
                {
                    for (int counter1 = 0; counter1 < 2; counter1++)
                    {
                        if (playerCards.ElementAt(counter1).GameValue == 10)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        public Boolean dealerHasBJ()
        {
            for (int counter = 0; counter < 2; counter++)
            {
                if (dealerCards.ElementAt(counter).Ace == true)
                {
                    for (int counter1 = 0; counter1 < 2; counter1++)
                    {
                        if (dealerCards.ElementAt(counter1).GameValue == 10)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        public Boolean dealerHasAce()
        {
            return dealerCards.ElementAt(0).Ace;
        }
        public void calcPlayerValue()
        {
            int sum = 0;
            for (int counter = 0; counter < playerCards.Count; counter++)
            {
                sum += playerCards.ElementAt(counter).GameValue;                   
            }
            if (sum > 21)
            {
                int counter = 0;
                while (counter < playerCards.Count)
                {
                    if ( (playerCards.ElementAt(counter).Ace == true) && (playerCards.ElementAt(counter).GameValue == 11) )
                    {
                        playerCards.ElementAt(counter).GameValue = 1;
                        sum -= 10;
                        break;
                    }
                    counter++;
                }
            }
            playerValue = sum;
        }
        public void calcNewPlayerValue()
        {
            //må brukes ved tillegg av hvert nytt playerkort.
            playerValue += playerCards.ElementAt(playerCards.Count - 1).GameValue;
            if (playerValue > 21)
            {
                int counter = 0;
                while (counter < playerCards.Count)
                {
                    if ((playerCards.ElementAt(counter).Ace == true) && (playerCards.ElementAt(counter).GameValue == 11))
                    {
                        playerCards.ElementAt(counter).GameValue = 1;
                        playerValue -= 10;
                        break;
                    }
                    counter++;
                }
            }
        }
        public void calcDealerValue()
        {
            int sum = 0;
            for (int counter = 0; counter < dealerCards.Count; counter++)
            {
                sum += dealerCards.ElementAt(counter).GameValue;
            }
            if (sum > 21)
            {
                int counter = 0;
                while (counter < dealerCards.Count)
                {
                    if ((dealerCards.ElementAt(counter).Ace == true) && (dealerCards.ElementAt(counter).GameValue == 11))
                    {
                        dealerCards.ElementAt(counter).GameValue = 1;
                        sum -= 10;
                        break;
                    }
                    counter++;
                }
            }
            dealerValue = sum;
        }
        public void calcNewDealerValue(){
            //må brukes ved tillegg av hvert nytt dealerkort.
            dealerValue += dealerCards.ElementAt(dealerCards.Count - 1).GameValue;
            if (dealerValue > 21)
            {
                int counter = 0;
                while (counter < dealerCards.Count)
                {
                    if ((dealerCards.ElementAt(counter).Ace == true) && (dealerCards.ElementAt(counter).GameValue == 11))
                    {
                        dealerCards.ElementAt(counter).GameValue = 1;
                        dealerValue -= 10;
                        break;
                    }
                    counter++;
                }
            }
        }
        public void calcNewSplitValue()
        {
            //må brukes ved tillegg av hvert nytt splitkort.
            splitValue += splitCards.ElementAt(splitCards.Count - 1).GameValue;
            if (splitValue > 21)
            {
                int counter = 0;
                while (counter < splitCards.Count)
                {
                    if ((splitCards.ElementAt(counter).Ace == true) && (splitCards.ElementAt(counter).GameValue == 11))
                    {
                        splitCards.ElementAt(counter).GameValue = 1;
                        splitValue -= 10;
                        break;
                    }
                    counter++;
                }
            }
        }
        public int getDealerValue()
        {
            return this.dealerValue;
        }
        public int getPlayerValue()
        {
            return this.playerValue;
        }
        public int getSplitValue()
        {
            return this.splitValue;
        }
        public Boolean playerCanSplit()
        {
            return (playerCards.ElementAt(0).Number == playerCards.ElementAt(1).Number) && (playerCards.ElementAt(0).Ace == false);
        }

    }
}
